const async = require('async');
const mysql = require('../../utils/db').mysql;
const laypage = require('laypage');
exports.page = function(req, res, next){
    const userInfo = req.session.userInfo;
    const flowerName = req.query.flowerName;
    let flowerTotal = 0;
    const url = req.url;
    const page = req.params.page || 1;
    if(userInfo.flowerAuthority != 1){
        res.statusCode = 404;
        res.send('404');
        return;
    }
    req.session.touch();
    async.auto({
        huayuList: function(callback){
            const limit = 20;
            const currentPage = limit * (page-1);
            if(flowerName){
                const queryStr = `SELECT SQL_CALC_FOUND_ROWS * FROM huayu WHERE title LIKE "%${flowerName}%" LIMIT ${currentPage},${limit};SELECT FOUND_ROWS() as total;`;// adminuser替换成表名
                mysql.getConnection(function(err, conn) {
                    conn.query(queryStr, function (err, result) {
                        if (err) {
                            callback(null, []);
                        }
                        let list = [];
                        list = list.concat(result[0]);
                        flowerTotal = result[1][0].total;
                        callback(null, list);
                        mysql.releaseConnection(conn);
                    });
                })
            }else{
                const queryStr = `SELECT SQL_CALC_FOUND_ROWS * FROM huayu LIMIT ${currentPage},${limit};SELECT FOUND_ROWS() as total;`;// adminuser替换成表名
                mysql.getConnection(function(err, conn) {
                    conn.query(queryStr, function (err, result) {
                        if (err) {
                            callback(null, []);
                        }
                        let list = [];
                        list = list.concat(result[0]);
                        flowerTotal = result[1][0].total;
                        callback(null, list);
                        mysql.releaseConnection(conn);
                    });
                })
            }
        }
    }, function(err, results){
        const laypageHtml = laypage({
            curr: page,
            url: url,
            pages: Math.ceil(flowerTotal / 20)
        });
        res.render('admin/huayuList', {
            userInfo: userInfo,
            results: results,
            channel: {
                type: 'manage-huayu',
                index: 1
            },
            laypageHtml: laypageHtml,
            flowerName: flowerName
        });
    })
}
exports.removeFlowerById = function(req, res, next){
    const userInfo = req.session.userInfo;
    if(userInfo.flowerAuthority != 1){
        res.send({
            code: 400,
            msg: '删除文章失败'
        });
        return;
    }
    const flowerId = req.body.id;
    const queryStr = `DELETE FROM huayu WHERE id="${flowerId}"`;
    mysql.getConnection(function(err, conn) {
        conn.query(queryStr, function (err, result) {
            if (err) {
                res.send({
                    code: 400,
                    msg: '删除失败'
                })
            } else {
                res.send({
                    code: 200,
                    msg: '删除成功'
                })
            }
            mysql.releaseConnection(conn);
        });
    })
}
exports.getXyhfSwiperList = function (req, res, next){
    res.send([
        {src: 'http://img.cssandjs.top/xyhf1.jpg', url: ''},
        {src: 'http://img.cssandjs.top/xyhf2.jpg', url: ''},
        {src: 'http://img.cssandjs.top/xyhf3.jpg', url: ''}
    ]);
};
exports.getFlowerList = function(req, res, next){
    const flowerName = req.query.flowerName;
    const url = req.url;
    const page = req.params.page || 1;
    const limit = 20;
    const currentPage = limit * (page-1);
    if(flowerName){
        const queryStr = `SELECT SQL_CALC_FOUND_ROWS * FROM huayu WHERE title LIKE "%${flowerName}%" LIMIT ${currentPage},${limit};SELECT FOUND_ROWS() as total;`;// adminuser替换成表名
        mysql.getConnection(function(err, conn) {
            conn.query(queryStr, function (err, result) {
                mysql.releaseConnection(conn);
                if (err) {
                    res.send([]);
                }
                let list = [];
                list = list.concat(result[0]);
                res.send(list);
            });
        })
    }else{
        const queryStr = `SELECT SQL_CALC_FOUND_ROWS * FROM huayu LIMIT ${currentPage},${limit};SELECT FOUND_ROWS() as total;`;// adminuser替换成表名
        mysql.getConnection(function(err, conn) {
            conn.query(queryStr, function (err, result) {
                mysql.releaseConnection(conn);
                if (err) {
                    res.send([]);
                }
                let list = [];
                list = list.concat(result[0]);
                res.send(list);
            });
        })
    }
}